package com.kstudio.videocapture.listimage;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;

import com.kstudio.videocapture.util.KLog;
import com.kstudio.videocapture.util.bitmapload.BitmapDoubleCacheParam;

public class KImageListAdapter extends ArrayAdapter<KImageListItem> {
    private final KLog log = new KLog("KImageList");

    private final Context mContext;
    KImageListLoadBitmap mLoadBitmap;

    private int mColumWidth;
    private int mColumHeight;

    public KImageListAdapter(Context context, KImageListLoadBitmap loadBitmap, ArrayList<KImageListItem> objects, int columWidth, int columHeight) {
        super(context, 0, objects);

        this.mContext = context;
        this.mLoadBitmap = loadBitmap;
        this.mColumWidth = columWidth;
        this.mColumHeight = columHeight;
        loadBitmap.setBitmapSize(mColumWidth - mColumWidth / 5, mColumHeight - mColumHeight / 5);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        KImageListViewAlbumIcon view = (KImageListViewAlbumIcon) convertView;
        KImageListViewWrapper   viewWrapper;

        if(view == null) {
            view = new KImageListViewAlbumIcon(mContext);
            view.setLayoutParams(new GridView.LayoutParams(mColumWidth,mColumHeight));
//            view.setAdjustViewBounds(false);
//            view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//            view.setBackgroundResource(R.drawable.kimage_icon_bg);
            viewWrapper = new KImageListViewWrapper(view);
            view.setTag(viewWrapper);
        } else {
            viewWrapper = (KImageListViewWrapper)view.getTag();
        }

        final KImageListItem data = (KImageListItem) this.getItem(position);
        if(data != null) {
            viewWrapper.setPosition(position);
            Bitmap bm = mLoadBitmap.get(new BitmapDoubleCacheParam(data.getPath()), mLoadBitmap.createParam(viewWrapper, position));

            viewWrapper.setLoadBitmap(bm);
        }

        return view;
    }
}
